#!/bin/bash
set -x
start_secs=$SECONDS
export ii=$II
export i=$I
tag=$1
dir=$2
if [[ -z "$dir" ]] ; then
  echo "Must give dir" >&2
  exit 1
fi
if [[ -d $ii/$dir ]] ; then
	cd $ii/$dir
else
	cd $dir
fi
if (( $? != 0 )) ; then
  echo "Cannot cd to $ii/$dir" >&2
  exit 1
fi
#test tmpa=$ii/tmpa
tmpa=$ii/whyall
if [[ -n "$tag" ]] ; then
  tag_parms="-r $tag"
fi
$CVS_LOC/cvs update -P $tag_parms . > $tmpa 2>&1
if (( $? != 0 )) ; then
  echo "Cannot cvs $ii/$dir" >&2
  grep -q ' src/java/com/hp' $tmpa 2>&1
  if (( $? == 0 )) ; then
    echo "Get src from cvs for $ii/$dir continuing"
  else
    echo "Cannot cvs $ii/$dir" >&2
		more $tmpa
    exit 1
  fi
fi
/bin/grep '^[?A]' $tmpa | \
    /bin/sed -e "s#^. #:!\$I/addcvs.ksh $dir/#g" > $ii/whyadd
/bin/grep '^[PU]' $tmpa | \
    /bin/sed -e "s#^. #:!\$I/synccvs.ksh $dir/#g" > $ii/whyupd
/bin/grep '^[?ACM]' $tmpa | \
    /bin/sed -e "s#^. #:!\$I/commitcvs.ksh -r \"$tag\" $dir/#g" > $ii/whycmt
echo "\":e! whycvs.diff" > $ii/whydiff
echo ":!rm -f whycvs.diff" >> $ii/whydiff
/bin/grep '^M' $tmpa | \
    /bin/sed -e "s#^. #:!\$I/diffcvs.ksh $dir/#g" >> $ii/whydiff
end_secs=$SECONDS
echo \
  "ELAPSED=$(( (end_secs - start_secs)/60 )):$(( (end_secs - start_secs)%60 ))"
